package br.com.foxinline.manager.pesquisar;

import br.com.foxinline.modelo.Saida;
import br.com.foxinline.modelo.TipoSaida;
import br.com.foxinline.servico.SaidaServico;
import br.com.foxinline.servico.TipoSaidaServico;
import br.com.foxinline.utilitario.Mensagem;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author cadomeireles
 */
@ViewScoped
@ManagedBean
public class ManagerPesquisarSaida implements Serializable {
    
    @EJB
    SaidaServico saidaServico;
    @EJB
    TipoSaidaServico tipoSaidaServico;
    
    private Saida saidaPesquisar;
    private Saida saidaDeletar;
    private Saida saidaVisualizar;    
    private List<Saida> saidas;
    
    private String visualizarId;

    @PostConstruct
    public void init() {
        instanciar();
        delegar();
    }

    public void instanciar() {
        this.saidaPesquisar = new Saida();
        this.saidaDeletar = new Saida();
        this.saidas = new ArrayList<Saida>();
    }

    private void delegar() {
        Map<String, String> parametros = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        this.visualizarId = parametros.get("saidaId");

        if (visualizarId != null) {
            this.saidaVisualizar = saidaServico.pesquisar(Long.parseLong(visualizarId));
        }
    }

    public void pesquisar() {
        this.saidas = saidaServico.pesquisar(this.saidaPesquisar);
    }
    
    public void limpar() {
        init();
    }
    
    public void deletar() {
        saidaServico.deletar(this.saidaDeletar);
        pesquisar();
        
        Mensagem.messagemInfo("Saída excluído com sucesso!");
    }

    public List<TipoSaida> autocompletarTipoSaida(String query) {
        return tipoSaidaServico.autocompletar(query);
    }    
    
    public Saida getSaidaPesquisar() {
        return saidaPesquisar;
    }

    public void setSaidaPesquisar(Saida saidaPesquisar) {
        this.saidaPesquisar = saidaPesquisar;
    }

    public Saida getSaidaDeletar() {
        return saidaDeletar;
    }

    public void setSaidaDeletar(Saida saidaDeletar) {
        this.saidaDeletar = saidaDeletar;
    }

    public List<Saida> getSaidas() {
        return saidas;
    }

    public void setSaidas(List<Saida> saidas) {
        this.saidas = saidas;
    }

    public Saida getSaidaVisualizar() {
        return saidaVisualizar;
    }

    public void setSaidaVisualizar(Saida saidaVisualizar) {
        this.saidaVisualizar = saidaVisualizar;
    }

}
